package org.darchitect.ui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import org.darchitect.nn.som.SOMController;

public class UIUtil {

	public static void centerWindow(Frame window) {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (dim.width - window.getWidth()) / 2;
		int y = (dim.height - window.getHeight()) / 2;
		window.setLocation(x, y);
	}
	
	public static void centerWindow(JDialog dialog) {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (dim.width - dialog.getWidth()) / 2;
		int y = (dim.height - dialog.getHeight()) / 2;
		dialog.setLocation(x, y);
	}
	
	public static void savePreferences(Class c, String key, String value) {
		Preferences pref = Preferences.userNodeForPackage(c);
		pref.put(key, value);
	}
	
	public static String getPreferences(Class c, String key) {
		Preferences pref = Preferences.userNodeForPackage(c);
		return pref.get(key, null);
	}
	
	public static final int OPEN = 1, SAVE = 2;
	public static File chooseFile(int type) {
		JFileChooser fchooser = null;
		String lastOpenDirecotry = UIUtil.getPreferences(UIUtil.class, "openDirectory");
		String lastSaveDirecotry = UIUtil.getPreferences(UIUtil.class, "saveDirectory");
		String prop = null;
		if (type == OPEN) {
			if (lastOpenDirecotry == null)
				lastOpenDirecotry = lastSaveDirecotry;
			fchooser = new JFileChooser(lastOpenDirecotry);
			prop = "openDirectory";
		} else if (type == SAVE) {
			if (lastSaveDirecotry == null)
				lastSaveDirecotry = lastOpenDirecotry;
			fchooser = new JFileChooser(lastSaveDirecotry);
			prop = "saveDirectory";
		}
		File f = null;
		int r = fchooser.showOpenDialog(null);
		if (r == JFileChooser.APPROVE_OPTION) {
			f = fchooser.getSelectedFile();
			UIUtil.savePreferences(UIUtil.class, prop, fchooser.getSelectedFile().getParent());
		}
		return f;
	}
	
	public static void setNativeLookAndFeel() {
		String os = System.getProperty("os.name");
		if (os.startsWith("Mac OS X"))
			return;
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
//			UIManager.setLookAndFeel("it.unitn.ing.swing.plaf.macos.MacOSLookAndFeel");
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (UnsupportedLookAndFeelException e) {
			try {
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			} catch (ClassNotFoundException e1) {
			} catch (InstantiationException e1) {
			} catch (IllegalAccessException e1) {
			} catch (UnsupportedLookAndFeelException e1) {
			}
		}
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
		}
	}
	
	public static void executeAndShowMessage(Frame frame, String message, String messageTitle, 
			final ExecutableAndMessageShowable exec) {
		Dimension dim = new Dimension(600, 400);
		JLabel waitLbl1 = new JLabel(message);
		JPanel waitPnl = new JPanel();
		waitPnl.add(waitLbl1);
		final JDialog savingModalDialog = new JDialog(frame, true);
		savingModalDialog.setResizable(false);
		savingModalDialog.setTitle(messageTitle);
		savingModalDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		savingModalDialog.setSize(dim);
		savingModalDialog.add(waitPnl);
		savingModalDialog.pack();
		UIUtil.centerWindow(savingModalDialog);
		new Thread() {
			public void run() {
				exec.execute();
				if (savingModalDialog.isShowing())
					savingModalDialog.dispose();
			}
		}.start();
		savingModalDialog.setVisible(true);
	}
	
	public static interface ExecutableAndMessageShowable {
		
		public void execute();
		
	}
	
}
